package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import model.Database;
import model.Evento;

import org.freixas.jcalendar.JCalendarCombo;

import controller.ActionCreateEvent;
import controller.EditEventKeyListener;
import controller.Engine;
import controller.GhostTextField;



@SuppressWarnings("serial")
public class CreateEvent extends JFrame implements Observer {
	JPanel div;
	JPanel div1;
	JPanel div2;
	JPanel div3;
	JPanel datiEvento;
	JPanel desc_panel;
	JLabel desc_label;
	JPanel nome_panel;
	JLabel nome_label;
	JPanel luogo_panel;
	JLabel luogo_label;
	JPanel date_panel;
	JLabel date_label;
	public static JCalendarCombo cal;
	JPanel time_panel;
	JLabel time_label;
	JPanel ora_panel;
	JPanel min_panel;
	JButton upH;
	JButton downH;
	JLabel due_punti;
	JButton upM;
	JButton downM;
	public static JTextField ora;
	public static JTextField min;
	JPanel type_panel;
	JLabel type_label;
	public static JComboBox<String> type;
	JPanel photo;
	JPanel info;
	public static int posH = 0;
	public static int posM = 0;

	public static JTextField nome;
	public static JTextField luogo;
	public static JTextArea desc;

	static Date Data;
	JPanel policy;
	JLabel policy_label;
	static ButtonGroup group;
	JRadioButton pubblico;
	JRadioButton privato;

	public static Vector<Integer> invitato = new Vector<Integer>();
	static String[] tipologia = { "Evento", "Lavoro", "Compleanno", "Festa",
	"Vacanza" };

	JPanel buttons;
	JButton crea;
	JButton annulla;

	static ActionListener action = new ActionCreateEvent();
	static KeyListener textControl = new EditEventKeyListener();

	static JFrame crevent=null;
	static JLabel image = null;
	public static Evento Evento;

	// --------------------------------------per la selection:
	static int contactCount = 1;

	JPanel scrollContainer = new JPanel();
	JButton inv = new JButton();

	public static int selectedItem = 0;

	public CreateEvent(Evento evento) {
		crevent = this;
		Evento = evento;
		Evento.addObserver(this);
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				Engine.restartHome("CreateEvent");
			}
		});
		startCreateEvent();
	}
	@SuppressWarnings("unused")
	public void startCreateEvent() {
		//System.out.println("startReg ---> evento creata, id evento= "	+ Evento.getEvn_id());
		div = new JPanel();
		div1 = new JPanel();
		div2 = new JPanel();
		div3 = new JPanel();

		datiEvento = new JPanel();
		nome_panel = new JPanel();
		luogo_panel = new JPanel();
		date_panel = new JPanel();
		cal = new JCalendarCombo();
		time_panel = new JPanel();
		ora_panel = new JPanel();
		min_panel = new JPanel();
		ora = new JTextField();
		min = new JTextField();
		desc_panel = new JPanel();
		type_panel = new JPanel();
		type = new JComboBox<String>();
		photo = new JPanel();
		info = new JPanel();
		policy = new JPanel();
		buttons = new JPanel();

		luogo = new JTextField(Evento.getLuogo(),20);
		luogo.setName("luogo");
		luogo.addKeyListener(textControl);
		GhostTextField focus = new GhostTextField(luogo,
				"Via,luogoevento,100 - Ancona(AN)");

		desc = new JTextArea(Evento.getDescr());
		desc.setName("desc");
		desc.addKeyListener(textControl);
		nome = new JTextField(Evento.getNome(),20);
		nome.setName("nome");
		nome.addKeyListener(textControl);

		GhostTextField focus2 = new GhostTextField(nome,"Nome Evento");

		luogo.setMaximumSize(new Dimension(300,34));
		luogo.setPreferredSize(new Dimension(300,34));
		luogo.setMinimumSize(new Dimension(300,34));
		nome.setMaximumSize(new Dimension(300,34));
		nome.setPreferredSize(new Dimension(300,34));
		nome.setMinimumSize(new Dimension(300,34));
		cal.setMaximumSize(new Dimension(297, 34));
		cal.setPreferredSize(new Dimension(297, 34));
		cal.setMinimumSize(new Dimension(297, 34));

		upH = new JButton("+");
		upH.setMaximumSize(new Dimension(34, 34));
		upH.setPreferredSize(new Dimension(34, 34));
		upH.setMinimumSize(new Dimension(34, 34));
		upH.setMargin(new Insets(0,0,0,0));
		downH =  new JButton("_");
		downH.setMaximumSize(new Dimension(34, 34));
		downH.setPreferredSize(new Dimension(34, 34));
		downH.setMinimumSize(new Dimension(34, 34));
		downH.setMargin(new Insets(-6,0,6,0));
		due_punti = new JLabel(":");
		upM =  new JButton("+");
		upM.setMaximumSize(new Dimension(34, 34));
		upM.setPreferredSize(new Dimension(34, 34));
		upM.setMinimumSize(new Dimension(34, 34));
		upM.setMargin(new Insets(0,0,0,0));
		downM =  new JButton("_");
		downM.setMaximumSize(new Dimension(34, 34));
		downM.setPreferredSize(new Dimension(34, 34));
		downM.setMinimumSize(new Dimension(34, 34));
		downM.setMargin(new Insets(-6,0,6,0));
		crea = new JButton("Crea");

		annulla = new JButton("Annulla");

		group = new ButtonGroup();
		pubblico = new JRadioButton();
		pubblico.setName("public");
		pubblico.addActionListener(action);
		pubblico.setActionCommand("0");
		pubblico.setText("Pubblico");
		privato = new JRadioButton();
		privato.setName("private");
		privato.addActionListener(action);
		privato.setActionCommand("1");
		privato.setText("Privato");

		if (Evento.getLevel() == 0)
			pubblico.setSelected(true);
		else
			privato.setSelected(true);
		group.add(pubblico);
		group.add(privato);

		nome.setToolTipText("Inserisci il nome dell'evento");
		luogo.setToolTipText("Inserisci il luogo in cui si terrà l'evento");
		desc.setToolTipText("Scrivi la descrzione dell'evento");
		pubblico.setToolTipText("Un evento pubblico è visualizzabile da tutti gli iscritti a LoAD");
		privato.setToolTipText("Un evento privato è visualizzabile solo da te");

		crea.setName("next");
		annulla.setName("annulla");
		upH.setName("upH");
		downH.setName("downH");
		upM.setName("upM");
		downM.setName("downM");
		crea.addActionListener(action);
		crea.setMinimumSize(new Dimension (80,25));
		crea.setPreferredSize(new Dimension (80,25));
		crea.setMaximumSize(new Dimension (80,25));
		annulla.addActionListener(action);
		annulla.setMinimumSize(new Dimension (80,25));
		annulla.setPreferredSize(new Dimension (80,25));
		annulla.setMaximumSize(new Dimension (80,25));
		upH.addActionListener(action);
		downH.addActionListener(action);
		upM.addActionListener(action);
		downM.addActionListener(action);

		//costruisco la struttura del frame

		setPhoto(Evento.getType());
		photo.add(image);
		photo.setBackground(Color.white);

		for (int i=0;i<Database.Utente.getEvnComuni().length;i++)
			type.addItem(Database.Utente.getEvnComuni()[i]);
		for (int i = 0; i < Database.Utente.getEvnSettore().length; i++)
			type.addItem(Database.Utente.getEvnSettore()[i]);
		type.setSelectedItem(Evento.getType());
		type.setName("typeChange");
		type.addActionListener(action);

		type_label = new JLabel("Tipo");
		type_label.setMinimumSize(new Dimension(40,30));
		type_label.setPreferredSize(new Dimension(40,30));
		type_label.setMaximumSize(new Dimension(40,30));

		type_panel.setBackground(Color.white);
		type_panel.add(Box.createHorizontalStrut(1));
		type_panel.add(type_label);
		type_panel.add(type);

		nome_label = new JLabel("Nome");
		nome_label.setMinimumSize(new Dimension(60,30));
		nome_label.setPreferredSize(new Dimension(60,30));
		nome_label.setMaximumSize(new Dimension(60,30));

		nome_panel.setLayout(new BoxLayout(nome_panel,BoxLayout.X_AXIS));
		nome_panel.setBackground(Color.white);
		nome_panel.add(Box.createHorizontalStrut(5));
		nome_panel.add(nome_label);
		nome_panel.add(Box.createHorizontalStrut(20));
		nome_panel.add(nome);
		nome_panel.add(Box.createHorizontalStrut(5));

		luogo_label = new JLabel("Luogo");
		luogo_label.setMinimumSize(new Dimension(60,30));
		luogo_label.setPreferredSize(new Dimension(60,30));
		luogo_label.setMaximumSize(new Dimension(60,30));

		luogo_panel.setLayout(new BoxLayout(luogo_panel,BoxLayout.X_AXIS));
		luogo_panel.setBackground(Color.white);
		luogo_panel.add(Box.createHorizontalStrut(5));
		luogo_panel.add(luogo_label);
		luogo_panel.add(Box.createHorizontalStrut(20));
		luogo_panel.add(luogo);
		luogo_panel.add(Box.createHorizontalStrut(5));
		cal.setFocusable(false);
		cal.setDate(Evento.getDataCal());
		cal.setBackground(Color.white);
		cal.addActionListener(action);
		cal.setName("dateChange");

		date_label = new JLabel("Data");
		date_label.setMinimumSize(new Dimension(60,30));
		date_label.setPreferredSize(new Dimension(60,30));
		date_label.setMaximumSize(new Dimension(60,30));

		date_panel.setLayout(new BoxLayout(date_panel,BoxLayout.X_AXIS));
		date_panel.setBackground(Color.white);
		date_panel.add(Box.createHorizontalStrut(5));
		date_panel.add(date_label);
		date_panel.add(Box.createHorizontalStrut(21));
		date_panel.add(cal);
		date_panel.add(Box.createHorizontalStrut(5));
		ora.setText(Evento.ora);

		ora.setEditable(false);
		ora.setFocusable(false);

		ora_panel.setLayout(new GridLayout(3,1));
		ora_panel.setBackground(Color.white);
		ora_panel.setAlignmentX(CENTER_ALIGNMENT);
		ora_panel.setAlignmentY(CENTER_ALIGNMENT);
		ora_panel.add(upH);
		ora_panel.add(ora);
		ora_panel.add(downH);
		ora_panel.setMinimumSize(new Dimension(35,88));
		ora_panel.setPreferredSize(new Dimension(35,88));
		ora_panel.setMaximumSize(new Dimension(35,88));

		min.setText(Evento.min);
		min.setEditable(false);
		min.setFocusable(false);

		min_panel.setLayout(new GridLayout(3,1));
		min_panel.setBackground(Color.white);
		min_panel.setAlignmentX(CENTER_ALIGNMENT);
		min_panel.setAlignmentY(CENTER_ALIGNMENT);
		min_panel.add(upM);
		min_panel.add(min);
		min_panel.add(downM);
		min_panel.setMinimumSize(new Dimension(35,88));
		min_panel.setPreferredSize(new Dimension(35,88));
		min_panel.setMaximumSize(new Dimension(35,88));

		time_panel.setLayout(new BoxLayout(time_panel,BoxLayout.X_AXIS));
		time_panel.setBackground(Color.white);
		time_panel.add(Box.createHorizontalStrut(105));
		time_panel.add(ora_panel);
		time_panel.add(Box.createHorizontalStrut(9));
		time_panel.add(due_punti);
		time_panel.add(Box.createHorizontalStrut(9));
		time_panel.add(min_panel);
		time_panel.add(Box.createHorizontalStrut(105));

		time_label = new JLabel("Ora");
		time_label.setMinimumSize(new Dimension(60,30));
		time_label.setPreferredSize(new Dimension(60,30));
		time_label.setMaximumSize(new Dimension(60,30));

		JPanel time_contPanel = new JPanel();
		time_contPanel.setLayout(new BoxLayout(time_contPanel,BoxLayout.X_AXIS));
		time_contPanel.setBackground(Color.white);
		time_contPanel.add(Box.createHorizontalStrut(5));
		time_contPanel.add(time_label);
		time_contPanel.add(Box.createHorizontalStrut(20));
		time_contPanel.add(time_panel);
		time_contPanel.add(Box.createHorizontalStrut(5));

		policy_label = new JLabel("Visibilità");
		policy_label.setMinimumSize(new Dimension(60,30));
		policy_label.setPreferredSize(new Dimension(60,30));
		policy_label.setMaximumSize(new Dimension(60,30));

		policy.setLayout(new BoxLayout(policy,BoxLayout.X_AXIS));
		policy.setBackground(Color.white);
		policy.add(policy_label);
		policy.add(Box.createHorizontalStrut(70));
		policy.add(pubblico);
		policy.add(Box.createHorizontalStrut(60));
		policy.add(privato);
		policy.add(Box.createHorizontalStrut(60));

		desc.setPreferredSize(new Dimension (622,265));
		desc.setLineWrap(true);
		desc.setText(Evento.getDescr());

		desc_panel.add(desc);
		desc_panel.setBackground(Color.white);

		buttons.setBackground(Color.white);
		buttons.add(crea);
		buttons.add(annulla);

		datiEvento.setLayout(new BoxLayout(datiEvento, BoxLayout.Y_AXIS));
		datiEvento.setBackground(Color.white);
		datiEvento.add(Box.createVerticalStrut(5));
		datiEvento.add(nome_panel);
		datiEvento.add(luogo_panel);
		datiEvento.add(date_panel);
		datiEvento.add(time_contPanel);
		datiEvento.add(policy);

		div2.setLayout(new BoxLayout(div2, BoxLayout.Y_AXIS));
		div2.setBackground(Color.white);
		div2.add(photo);
		div2.add(type_panel);

		div3.setLayout(new BoxLayout(div3,BoxLayout.X_AXIS));
		div3.setBackground(Color.white);
		div3.add(div2);
		div3.add(datiEvento);

		div1.setLayout(new BoxLayout(div1, BoxLayout.Y_AXIS));
		div1.setBackground(Color.white);
		div1.add(div3);
		div1.add(desc_panel);
		div1.add(buttons);
		div1.add(Box.createVerticalStrut(5));

		// ---------------------------------------------aggiunta membri

		JPanel contact = new JPanel();
		JPanel scrollContainer1 = new JPanel();
		JPanel contactContainer = new JPanel();
		contactContainer.setLayout(new BoxLayout(contactContainer,BoxLayout.Y_AXIS));
		JScrollPane scroll1 = new JScrollPane(contactContainer,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		contact.setBackground(Color.white);
		scroll1.setMinimumSize(new Dimension(318,513));
		scroll1.setPreferredSize(new Dimension(318,513));
		scroll1.setMaximumSize(new Dimension(318,513));
		contactContainer.setBackground(Color.white);
		scroll1.setVisible(true);
		inv.setPreferredSize(new Dimension(340,27));
		inv.add(new JLabel("Rubrica:"));
		inv.setFocusable(false);
		inv.setContentAreaFilled(false);
		contactContainer.add(inv);
		if (!Database.Utente.getRubrica().isEmpty())
			for (int i = 0; i < Database.Utente.getRubrica().size(); i++) {
				JPanel contactPanel = new JPanel();
				contactPanel.setBackground(new Color(0, 0, 0, 0));
				JButton contatto = new JButton();
				for (int j = 0; j < invitato.size(); j++)
					if (invitato.get(j) == Database.Utente.getRubrica().get(i)
					.getID()) {
						contatto.setBackground(Color.green);
						break;
					}
				contatto.setMaximumSize(new Dimension(318,36));;
				JComponent comp = new JComponent(){
				};
				comp.setName("" + Database.Utente.getRubrica().get(i).getID());
				contactPanel.setLayout(new BoxLayout(contactPanel,
						BoxLayout.X_AXIS));
				contactPanel.add(Engine.setImage(Database.Utente.getRubrica()
						.get(i).getPhoto(), 50, 50));
				contactPanel.add(Box.createHorizontalStrut(5));
				contactPanel.add(new JLabel(Database.Utente.getRubrica().get(i)
						.getFirstname()
						+ " "
						+ Database.Utente.getRubrica().get(i).getSecondname()));
				contactPanel.add(Box.createHorizontalGlue());
				contatto.add(comp);
				contatto.add(contactPanel);
				contatto.addActionListener(action);
				// contatto.setAlignmentX(LEFT_ALIGNMENT);
				contatto.setName("contatto");
				contactContainer.add(contatto);
			}
		contactCount = contactContainer.getComponentCount();
		if (contactCount == 1) {
			JButton contatto = new JButton();
			contatto.setMaximumSize(new Dimension(318,24));
			contatto.add(new JLabel("Non sono presenti contatti nella tua rubrica!"));
			contatto.setContentAreaFilled(false);
			contactContainer.add(contatto);
			contactCount = contactContainer.getComponentCount();
		}
		contactContainer.add(Box.createVerticalGlue());

		scrollContainer1.add(scroll1);
		scrollContainer1.setBackground(Color.white);

		contact.setLayout(new BoxLayout(contact,BoxLayout.Y_AXIS));
		contact.add(Box.createVerticalStrut(1));
		contact.add(scrollContainer1);
		contact.setMinimumSize(new Dimension(350,565));
		contact.setPreferredSize(new Dimension(350,565));
		contact.setMaximumSize(new Dimension(350,565));

		div.setLayout(new BoxLayout(div, BoxLayout.X_AXIS));
		div.add(div1);

		int a;
		if (privato.isSelected()){
			div.add(contact);
			a=1000;
		}
		else{
			div.remove(contact);
			a=650;
		}
		crevent.add(div);

		crevent.setIconImage(Engine.immaginelog);
		crevent.setTitle("Nuovo Evento");
		//posizionatore del home frame
		crevent.setResizable(false);
		crevent.setVisible(true);
		crevent.setPreferredSize(new Dimension (a,592));
		crevent.pack();
		Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (schermo.width-1000)/2;
		int y = (schermo.height-592)/2;
		crevent.setLocation(x,y);
		crevent.requestFocus();

	}

	public static void setPhoto(String img) {
		image = Engine.setEventImage(img, 200, 200);
	}

	public static void stopCreateEvent() {
		crevent.dispose();
	}

	public static void allertInvalidDate() {
		JOptionPane
		.showMessageDialog(crevent,
				"Non puoi creare un evento con data inferiore a quella odierna!");

	}

	public static void allertInvalidName() {
		JOptionPane.showMessageDialog(crevent, "Il campo nome è vuoto!");
	}

	public static void allertInvalidLuogo() {
		JOptionPane.showMessageDialog(crevent, "Il campo luogo è vuoto!");
	}

	public static void allertInvalidDesc() {
		JOptionPane.showMessageDialog(crevent, "Il campo descrizione è vuoto!");
	}

	@SuppressWarnings("deprecation")
	public static void confirmEventProp(boolean flag) {
		if (nome.getForeground() != Color.lightGray)
			Evento.setNome(nome.getText());
		if (luogo.getForeground() != Color.lightGray)
			Evento.setLuogo(luogo.getText());
		if (desc.getForeground() != Color.lightGray)
			Evento.setDescr(desc.getText());
		Evento.setTime(posH, posM);
		String giorno;
		String mese;
		if (cal.getDate().getDate() < 10)
			giorno = "0" + cal.getDate().getDate();
		else
			giorno = "" + cal.getDate().getDate();
		if ((cal.getDate().getMonth() + 1) < 10)
			mese = "0" + (cal.getDate().getMonth() + 1);
		else
			mese = "" + (cal.getDate().getMonth() + 1);
		String day = giorno + "/" + mese + "/"
				+ (cal.getDate().getYear() + 1900);
		// System.out.println(day);
		Evento.setDay(day);
		Evento.setDataCal(cal.getDate());
		Evento.setLevel(Integer
				.valueOf(group.getSelection().getActionCommand()));
		Evento.setType((String) type.getSelectedItem());
		selectedItem = type.getSelectedIndex();
		if (Evento.getLevel() == 0) {
			if (flag)
				for (int i = 0; i < invitato.size(); i++)
					Evento.removeInvitato(String.valueOf(invitato.get(i)));
			invitato.removeAllElements();
		} else if (flag)
			for (int i = 0; i < invitato.size(); i++)
				Evento.addInvitato(String.valueOf(invitato.get(i)));

	}

	public static void Uppdater() {
		//System.out.println("Richiesta Aggiornamento Model");
		Evento.uppdater();
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		//System.out.println("Aggiornamento View Effettuato");
		crevent.remove(div);
		validate();
		startCreateEvent();
	}
}
